﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace DotNetMigrations
{
	/// <summary>
	/// Utility methods for fetching specific arguments from an array.
	/// </summary>
	public class ArgFetcher
	{
		public static int GetFirstNumericArg(string[] args)
		{
			foreach (string a in args)
			{
				int i;
				if (int.TryParse(a, out i))
					return i;
			}

			return -1;
		}

		public static string GetConnectionString(string[] args)
		{
			int argCount = args.GetLength(0);
			if (argCount == 0)
				return null;

			for (int i = 0; i < argCount; i++)
			{
				string s = args[i];

				//	skip any numeric arguments
				int j;
				if (int.TryParse(s, out j) == true)
					continue;

				//	first check if there is a connection string
				//	by that name in the config file, if so, return that
				ConnectionStringSettings conn = ConfigurationManager.ConnectionStrings[s];
				if (conn != null)
					return conn.ConnectionString;
				else
					//	else, lets just assume this is argument is
					//	a specific connection string itself and return it
					return s;
			}

			return null;
		}
	}
}
